<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear='getGoodsList' >
            <el-button slot="append" icon="el-icon-search" @click='getGoodsList'></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click='goAddpage'>添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品详情数据表 -->
      <el-table :data="goodList" style="width: 100%" border>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="90"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="90"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="160">
          <template slot-scope="scope">{{scope.row.add_time| dataFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-button type="warning" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click='removeGoods(scope.row)'>删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodList: [],
      total: 0
    }
  },
  methods: {
    async getGoodsList() {
      let { data: res } = await this.axios.get('goods', { params: this.queryInfo })
      if (res.meta.status != 200) return this.$message.error('请求出错')
      // this.$message.success('获取数据成功')
      this.goodList = res.data.goods
      this.total = res.data.total
      //   console.log(res)
    },
    // 分页功能
    handleSizeChange(val) {
        // console.log(`每页 ${val} 条`);
        this.queryInfo.pagesize=val;
        this.getGoodsList();
      },
      handleCurrentChange(val) {
        // console.log(`当前页: ${val}`);
        this.queryInfo.pagenum=val;
        this.getGoodsList();
      },
    //   根据id删除商品
    async removeGoods(row){
        let confireRulte=await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err);
      if(confireRulte!='confirm') return this.$message.info('已经取消删除');
        // 接下来调用接口去删除数据
        // console.log(row.goods_id);
        
       let {data:res}= await this.axios.delete(`goods/${row.goods_id}`)
    //    console.log(res);
    if(res.meta.status!=200) return this.$message.error('删除失败')
      this.$message.success('已经删除成功')
      this.getGoodsList();
    },
    // 点击添加商品跳转到添加页面
    goAddpage(){
        this.$router.push('/goods/add')
    }
  },
  created() {
    this.getGoodsList()
  }
}
</script>


<style lang="less" scoped>
.el-card {
  margin-top: 15px;
}
.el-button {
  margin-left: 15px;
}
</style>
